"use strict";
cc._RF.push(module, 'b9219kyboxNxLXr6ljcHGQF', 'level_cardComposition_cardGroup');
// scripts/levelBundles/level_cardComposition/base/level_cardComposition_cardGroup.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../../../engines/gameEngine");
var level_cardCompositionConst_1 = require("../const/level_cardCompositionConst");
var level_cardComposition_card_1 = require("./level_cardComposition_card");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var level_cardComposition_cardGroup = /** @class */ (function (_super) {
    __extends(level_cardComposition_cardGroup, _super);
    function level_cardComposition_cardGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**当前组中的所有卡牌 */
        _this.cards = [];
        _this.lockSp = null;
        _this.unlockSp = null;
        _this.adNode = null;
        /**组类型 */
        _this.groupType = level_cardCompositionConst_1.level_cardCompositionGroupTypeE.normal;
        _this.lbTarget = null;
        _this.synsicNode = null;
        /**最大卡牌数量 */
        _this.maxLength = 10;
        /**卡牌的y间隔 */
        _this.cardIntervalY = 38;
        /**是否是锁定状态 */
        _this.lockFlag = false;
        /**卡牌的高度 */
        _this.cardHeight = 127;
        /**顶部间隔 */
        _this.topInterval = 10;
        /**关卡 */
        _this.level = null;
        _this.color = null;
        return _this;
    }
    level_cardComposition_cardGroup.prototype.onLoad = function () {
        this.initGroup();
    };
    level_cardComposition_cardGroup.prototype.setGroupType = function (type) {
        this.groupType = type;
        this.initGroup();
    };
    level_cardComposition_cardGroup.prototype.initGroup = function () {
        this.lockSp.active = false;
        this.adNode.active = false;
        this.lockFlag = false;
        switch (this.groupType) {
            case level_cardCompositionConst_1.level_cardCompositionGroupTypeE.unlock: {
                this.lockFlag = true;
                this.lockSp.active = true;
                break;
            }
            case level_cardCompositionConst_1.level_cardCompositionGroupTypeE.temp: {
                this.adNode.active = true;
                this.lockFlag = true;
                break;
            }
        }
    };
    level_cardComposition_cardGroup.prototype.bindData = function (level) {
        this.level = level;
    };
    /**加入一张卡牌 */
    level_cardComposition_cardGroup.prototype.pushCard = function (card, isJudge, hasAni, soundFlag) {
        var _this = this;
        if (isJudge === void 0) { isJudge = true; }
        if (hasAni === void 0) { hasAni = true; }
        if (soundFlag === void 0) { soundFlag = true; }
        if (!isJudge || this.checkCanPushCard(card)) {
            var zindex_1 = this.cards.length;
            var cardPos = this.getCardPos(this.cards.length);
            this.cards.push(card);
            card.setGroup(this);
            return this.level.pushCardAni(card.node, cardPos, hasAni, soundFlag).then(function () {
                card.node.zIndex = zindex_1;
                _this.synsicNode.active = _this.checkCanSynthesis();
            });
        }
    };
    /**获取卡牌的相对位置 */
    level_cardComposition_cardGroup.prototype.getCardPos = function (cardIndex) {
        var py = ((-(this.cardIntervalY * cardIndex)) - (this.cardHeight / 2)) - this.topInterval;
        var px = 0;
        var targetPos = this.node.convertToWorldSpaceAR(cc.v2(px, py));
        return targetPos;
    };
    /**判断是否可以加入卡牌 */
    level_cardComposition_cardGroup.prototype.checkCanPushCard = function (card) {
        if (this.cards.length >= this.maxLength) {
            return false;
        }
        var lastCard = this.getLastCard();
        if (lastCard) {
            return this.checkCardVaild(card, lastCard);
        }
        else {
            return true;
        }
    };
    /**删除一张卡牌 */
    level_cardComposition_cardGroup.prototype.spliceCard = function (card) {
        var cardIndex = this.findCardIndex(card);
        if (cardIndex > -1) {
            card.onSpliceToGroup();
            this.cards.splice(cardIndex, 1);
        }
        if (this.cards.length == 0 && this.groupType == level_cardCompositionConst_1.level_cardCompositionGroupTypeE.temp) {
            this.checkTempGroupToBack();
        }
        this.synsicNode.active = this.checkCanSynthesis();
    };
    /**通过card找出卡牌的索引 */
    level_cardComposition_cardGroup.prototype.findCardIndex = function (card) {
        var cardIndex = this.cards.findIndex(function (c) {
            return card.uuid == c.uuid;
        });
        return cardIndex;
    };
    level_cardComposition_cardGroup.prototype.spliceAllCards = function () {
        this.cards = [];
        this.synsicNode.active = this.checkCanSynthesis();
    };
    /**获取所有卡牌 */
    level_cardComposition_cardGroup.prototype.getCards = function () {
        return this.cards;
    };
    /**获取最后一张卡牌 */
    level_cardComposition_cardGroup.prototype.getLastCard = function () {
        return this.cards[this.cards.length - 1];
    };
    /**获取所有可以移动的卡牌 */
    level_cardComposition_cardGroup.prototype.getMoveCards = function () {
        var card = this.getLastCard();
        if (!card) {
            return this.cards;
        }
        var vaildCards = [card];
        for (var i = this.cards.length - 2; i >= 0; i--) {
            var iCard = this.cards[i];
            if (this.checkMoveCardVaild(iCard, card)) {
                vaildCards.push(iCard);
            }
            else {
                break;
            }
        }
        return vaildCards;
    };
    /**设置可以移动卡牌的选择*/
    level_cardComposition_cardGroup.prototype.setMoveCardSelect = function (isSelect) {
        var cards = this.getMoveCards();
        for (var _i = 0, cards_1 = cards; _i < cards_1.length; _i++) {
            var card = cards_1[_i];
            var cs = card.getComponent(level_cardComposition_card_1.default);
            cs.setSelect(isSelect);
        }
    };
    /**卡牌合成 */
    level_cardComposition_cardGroup.prototype.cardSynthesis = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        if (!_this.checkCanSynthesis()) {
                            resolve(false);
                            return;
                        }
                        // 合成
                        var oneTime = 0.03;
                        var targetPos = _this.cards[0].node.getPosition();
                        _this.cards.forEach(function (card, index) {
                            var node = card.node;
                            card.setSelect(false);
                            //@ts-ignore
                            cc.tween(node).delay(oneTime * _this.maxLength - (index + 1) * oneTime).to(oneTime * (index + 1), { position: targetPos }).call(function () {
                                _this.level.putOneCard(node);
                            }).start();
                        });
                        _this.scheduleOnce(function () {
                            _this.spliceAllCards();
                            resolve(true);
                        }, 9 * oneTime);
                    })];
            });
        });
    };
    level_cardComposition_cardGroup.prototype.checkCanSynthesis = function () {
        if (this.cards.length < this.maxLength) {
            return false;
        }
        var cardComposition = null;
        for (var i = 0; i < this.cards.length; i++) {
            var card = this.cards[i];
            var cComposition = card.cardComposition;
            if (cComposition == null) {
                return false;
            }
            if (cardComposition == null)
                cardComposition = cComposition;
            if (cComposition != cardComposition) {
                return false;
            }
            if (card.cardType == level_cardCompositionConst_1.level_cardCompositionCardType.query) {
                return false;
            }
        }
        if (cardComposition == null) {
            return false;
        }
        return true;
    };
    /**判断卡牌是否合法 */
    level_cardComposition_cardGroup.prototype.checkCardVaild = function (checkCard, judgeCard) {
        var checkType = checkCard.cardType;
        var judgeType = judgeCard.cardType;
        var checkComposition = checkCard.cardComposition;
        var judgeComposition = judgeCard.cardComposition;
        if (checkType == judgeType) {
            if (checkType == level_cardCompositionConst_1.level_cardCompositionCardType.normal) {
                return checkComposition == judgeComposition;
            }
            return true;
        }
        else {
            if (checkType == level_cardCompositionConst_1.level_cardCompositionCardType.universal && judgeType == level_cardCompositionConst_1.level_cardCompositionCardType.normal) {
                return true;
            }
            if (checkType == level_cardCompositionConst_1.level_cardCompositionCardType.obstacles) {
                return true;
            }
            return false;
        }
    };
    level_cardComposition_cardGroup.prototype.checkMoveCardVaild = function (checkCard, judgeCard) {
        var checkType = checkCard.cardType;
        var judgeType = judgeCard.cardType;
        var checkComposition = checkCard.cardComposition;
        var judgeComposition = judgeCard.cardComposition;
        if (checkType == judgeType) {
            if (checkType == level_cardCompositionConst_1.level_cardCompositionCardType.normal) {
                return checkComposition == judgeComposition;
            }
            return true;
        }
        else {
            return false;
        }
    };
    level_cardComposition_cardGroup.prototype.getMaxCardsLength = function () {
        return this.maxLength;
    };
    level_cardComposition_cardGroup.prototype.getCurCardsLength = function () {
        return this.cards.length;
    };
    level_cardComposition_cardGroup.prototype.setCanUnlock = function () {
        //设置为可解锁状态
        this.lockSp.active = false;
        this.unlockSp.active = true;
        this.unlockAni();
    };
    level_cardComposition_cardGroup.prototype.unlockAni = function () {
        cc.Tween.stopAllByTarget(this.unlockSp);
        var tween = cc.tween(this.unlockSp).to(0.2, { opacity: 200 }).to(0.2, { opacity: 255 });
        cc.tween(this.unlockSp).repeatForever(tween).start();
    };
    /**
     * 点击解锁卡槽
     */
    level_cardComposition_cardGroup.prototype.onBtnUnlockGroup = function () {
        gameEngine_1.default.getManagers().soundMgr.playSound("cardComposition/click");
        this.unlockSp.active = false;
        this.lockFlag = false;
    };
    /**点击解锁临时卡槽 */
    level_cardComposition_cardGroup.prototype.onBtnTempGroup = function () {
        var _this = this;
        gameEngine_1.default.getManagers().soundMgr.playSound("cardComposition/click");
        var unlockFun = function () {
            _this.lockFlag = false;
            _this.adNode.active = false;
        };
        if (gameEngine_1.default.getServices().userSrv.getCoins() > 0) {
            gameEngine_1.default.getServices().userSrv.setCoins(-1, true);
            unlockFun();
        }
        else {
            gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(function () {
                unlockFun();
            }, function () {
            }, null, "看广告解锁临时组");
        }
    };
    /**点击不可解锁的组 */
    level_cardComposition_cardGroup.prototype.onBtnLock = function () {
    };
    level_cardComposition_cardGroup.prototype.changeTempGroupToBack = function () {
        this.adNode.active = true;
    };
    /**
     * 判断临时组是否还原
     */
    level_cardComposition_cardGroup.prototype.checkTempGroupToBack = function () {
        var canDealGroup = this.level.getRandLeisureGroup();
        if (canDealGroup) {
            var cards = this.cards.concat();
            for (var _i = 0, cards_2 = cards; _i < cards_2.length; _i++) {
                var card = cards_2[_i];
                this.spliceCard(card);
                canDealGroup.pushCard(card, false, true);
            }
            this.spliceCardEnd(cards);
            canDealGroup.pushCardEnd(cards);
            this.changeTempGroupToBack();
        }
    };
    /**设置target文本 */
    level_cardComposition_cardGroup.prototype.setTargetLb = function (targetText) {
        this.lbTarget.string = targetText;
    };
    level_cardComposition_cardGroup.prototype.changeState = function () {
        var cards = this.getCards();
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            if (card.cardType == level_cardCompositionConst_1.level_cardCompositionCardType.query) {
                var isVaild = true;
                for (var j = i + 1; j < cards.length; j++) {
                    var c = cards[j];
                    if (c.cardType != level_cardCompositionConst_1.level_cardCompositionCardType.query) {
                        isVaild = false;
                        break;
                    }
                }
                if (isVaild) {
                    for (var j = i; j < cards.length; j++) {
                        var c = cards[j];
                        c.cardToflip();
                    }
                }
            }
        }
    };
    level_cardComposition_cardGroup.prototype.pushCardEnd = function (cards) {
        cards.forEach(function (card) {
            card.onPushToGroup();
        });
    };
    level_cardComposition_cardGroup.prototype.spliceCardEnd = function (cards) {
        cards.forEach(function (card) {
            card.onSpliceToGroup();
        });
    };
    __decorate([
        property({ type: cc.Node, tooltip: "锁图片" })
    ], level_cardComposition_cardGroup.prototype, "lockSp", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "可解锁图片" })
    ], level_cardComposition_cardGroup.prototype, "unlockSp", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "广告节点" })
    ], level_cardComposition_cardGroup.prototype, "adNode", void 0);
    __decorate([
        property({ type: cc.Label, tooltip: "合成文本" })
    ], level_cardComposition_cardGroup.prototype, "lbTarget", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "可合成粒子效果" })
    ], level_cardComposition_cardGroup.prototype, "synsicNode", void 0);
    level_cardComposition_cardGroup = __decorate([
        ccclass
    ], level_cardComposition_cardGroup);
    return level_cardComposition_cardGroup;
}(cc.Component));
exports.default = level_cardComposition_cardGroup;

cc._RF.pop();